package com.huifaapp.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.view.menu.MenuAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.ApplicationsRecordAdapter;
import com.huifaapp.mall.adapter.HomeAdapter;
import com.huifaapp.mall.adapter.MenuuAdapter;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.CategoryBean;
import com.huifaapp.mall.uitils.DimenUtils;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 *分类页面
 */

public class Tmallclassify_Fragment extends Fragment {
    private List<String> menuList = new ArrayList<>();
    private List<CategoryBean.DataBean> homeList = new ArrayList<>();
    private List<Integer> showTitle;
    private ListView lv_menu;
    private ListView lv_home;
    private MenuuAdapter menuAdapter;
    private HomeAdapter homeAdapter;
    private int currentItem;
    private TextView tv_title;
    private View view;
    private TextView edit_search;
    private List<String>list=new ArrayList<>();
    ImageView iv_fenlei_back;
    public Tmallclassify_Fragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tmallclassify, container,false);
        Fresco.initialize(getActivity());
        View v = getActivity().getWindow().findViewById(Window.ID_ANDROID_CONTENT);
        int height = v.getHeight();
        int categoryHeight = height- DimenUtils.dip2px(getActivity(),40);
        initView(v);
        homeAdapter.setCategoryHeight(categoryHeight);
//        loadData(ApiManager.BASE_NIUPAI_URL+"MyListView");
        loadData(ApiManager.BASE_NIUPAI_URL+"goods/categorys");

        iv_fenlei_back= (ImageView) view.findViewById(R.id.iv_fenlei_back);
        iv_fenlei_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (getActivity() instanceof MainFragment) {
                        MainFragment mainFragment = (MainFragment) getActivity();
                        mainFragment.bottomTabBar.setSelectItem1("首页");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return view;
    }

    private void loadData(String url) {//                .addParams("type", "yh")
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
//                    @Override
//                    public Object parseNetworkResponse(Response response) throws IOException {
//
//                        return null;
//                    }

                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        String jsonContent = response;
                        Gson gson = new Gson();
                        final CategoryBean categoryBean = gson.fromJson(jsonContent, CategoryBean.class);
                        if (categoryBean != null && categoryBean.getData().size() > 0) {
                            showTitle = new ArrayList<>();
                            for (int i = 0; i < categoryBean.getData().size(); i++) {
                                CategoryBean.DataBean dataBean = categoryBean.getData().get(i);
                                menuList.add(dataBean.getName());
                                showTitle.add(i);
                                homeList.add(dataBean);
                                list.add(dataBean.getCodeid());

                            }
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
//                                    tv_title.setText(categoryBean.getData().get(0).getName());
                                    menuAdapter.notifyDataSetChanged();
                                    homeAdapter.notifyDataSetChanged();
                                }
                            });

                        }

                    }


                });
    }
    private void initView(View v) {
        lv_menu = (ListView) view.findViewById(R.id.lv_menu);
        tv_title = (TextView)view. findViewById(R.id.tv_titile);
        lv_home = (ListView)view. findViewById(R.id.lv_home);
        edit_search= (TextView) view.findViewById(R.id.edit_search);
        edit_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),SearActivity.class);
                startActivity(intent);
            }
        });
        menuAdapter = new MenuuAdapter(getActivity(),menuList);
        lv_menu.setAdapter(menuAdapter);

        homeAdapter = new HomeAdapter(getActivity(), homeList,list);
        lv_home.setAdapter(homeAdapter);

        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                menuAdapter.setSelectItem(position);
                menuAdapter.notifyDataSetInvalidated();
//                tv_title.setText(menuList.get(position));
                lv_home.setSelection(showTitle.get(position));
            }
        });

    }


}
